﻿using AutoMapper;
using MediatR;
using ProductionLine.API.Application.Command.Dispatch;
using ProductionLine.Domain.BOM;
using ProductionLine.Domain.Car;
using ProductionLine.Domain.Dispatch;
using ProductionLine.ErrorCode;
using ProductionLine.Infrastructure.Implement;

namespace ProductionLine.API.Application.Handler.Dispatch
{
    public class AddDispatchHandler : IRequestHandler<AddDispatchCommand, APIResult<int>>
    {
        DatabaseContext db;
        IMapper mapper;

        public AddDispatchHandler(DatabaseContext db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(AddDispatchCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> res = new APIResult<int>();
            var model = mapper.Map<TaskDispatchModel>(request);
            int r = db.SqlSugar.Insertable(model).ExecuteCommand();
            res.Msg = "添加工单任务成功";
            res.Data = r;
            res.Code = APIEnum.Success;
            return await Task.FromResult(res);
        }
    }
}
